<?php
require('header.php');
require("settings.php");
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

	// Connect and select:
	$dbc = mysql_connect($server, $username, $password);
	mysql_select_db('sjoconnect4', $dbc);
	
	// Validate the form data:
	$problem = FALSE;
	if (!empty($_POST['user']) && !empty($_POST['email']) && !empty($_POST['pass']) && !empty($_POST['confirm'])) {
			$user = '"'.$_REQUEST["user"].'"';
			$user = strtolower($user);
			$email = '"'.$_REQUEST["email"].'"';
			$pass = '"'.(md5($_REQUEST["pass"])).'"';
			$confirm='"'.(md5($_REQUEST["confirm"])).'"';
	} 
     
	else {
		print '<p style="color: red;">The fields haven´t been filled.</p>';
		echo '<a href="index.php">Return to the register</a>';
		$problem = TRUE;
	}

	if ($confirm !=$pass)
    {

	print '<p style="color: red;">The password doesn´t match with the confirm password.</p>';
	echo '<a href="index.php">Return to the register</a>';
	$problem=TRUE;
    }

	if (!$problem) {

		// Define the query:
		$query = "INSERT INTO Players (PlayerID, UserName, Email, Password, PlayedGames, WonGames) VALUES (0,$user,$email,$pass,0,0)";
		
		
		// Execute the query:
		if (@mysql_query($query, $dbc)) {
			print '<p>User registered!</p>';
		} 
		else {
			print '<p style="color: red;">Could not sign in because the user ' . $user . 'or the email ' . $email . 'did exist yet </p>';
             echo '<a href="index.php">Return to the register</a>';


         }
	
	} // No problem!

	mysql_close($dbc); // Close the connection.	
}
else{

echo <<<_HTML
	
	<form action="index.php" method="post">
	<label>Username:</label><br />
	<input type="text" name="user"/><br />
	<label>E-mail address:</label><br />
	<input type="text" name="email"/><br />
	<label>Password:</label><br />
	<input type="password" name="pass"/><br />
	<label>Confirm password:</label><br />
	<input type="password" name="confirm"/><br /><br />

	<input type="submit" value="Sign-In" />

	</form>

_HTML;
}
?>

<?php
require('footer.php');
?>